import type { PowerResType } from '@/hooks/useRequestPowerAfterInit';
import type { ArtColumn } from 'ali-react-table';
import { Button, Checkbox } from 'antd';
import _ from 'lodash';

const columns = ({
  view,
  exportFile,
  changeCheckbox,
}: {
  view: (row: any) => void;
  exportFile: (row: any) => void;
  changeCheckbox: (val: boolean, index: number) => void;
}): ArtColumn[] => [
  {
    code: 'stationName',
    name: '场站',
    width: 120,
    align: 'left',
    features: { autoRowSpan: true },
  },
  {
    code: 'attachmentName',
    name: '名称',
    align: 'left',
    render: (value, row, index) => {
      if (!value) {
        return '-';
      }
      return (
        <Checkbox
          checked={row.checked ? true : false}
          onChange={(e) => changeCheckbox(e.target.checked, index)}
        >
          {value}
        </Checkbox>
      );
    },
  },
  {
    code: 'options',
    name: '操作',
    align: 'center',
    width: 100,
    render: (_, row) => {
      if (!row.attachmentName && !row.attachmentPath) {
        return;
      }
      return (
        <>
          <Button type="link" size="small" onClick={() => exportFile(row)}>
            下载
          </Button>
          <Button type="link" size="small" onClick={() => view(row)}>
            预览
          </Button>
        </>
      );
    },
  },
];

const buildTableData = (data: any, stationList: PowerResType[]) => {
  const tableData: any = {};
  const tabs: any = [];
  if (data && Object.keys(data).length > 0) {
    for (const i in data) {
      const tab = {
        label: i,
        value: i,
      };
      tabs.push(tab);

      const keys = Object.keys(data[i]);
      tableData[i] = [];
      if (keys && keys.length > 0) {
        keys.forEach((k) => {
          const item: PowerResType = _.find(stationList, (n: PowerResType) => n.stationId === k);
          const stationName = item ? item.stationName : '';
          const arr = data[i][k];
          if (arr && arr.length > 0) {
            arr.forEach((row: any) => {
              tableData[i].push({ ...row, stationName });
            });
          } else {
            tableData[i].push({ stationName, stationId: k });
          }
        });
      }
    }
  }

  return {
    tabs,
    tableData,
  };
};

const downFile = (fileUrl: string, fileName = '未命名') => {
  if (!fileUrl) return;
  const a = document.createElement('a');
  a.href = fileUrl;
  a.download = fileName;
  document.body.appendChild(a);
  const evt = document.createEvent('MouseEvents');
  evt.initEvent('click', false, false);
  a.dispatchEvent(evt);
  document.body.removeChild(a);
};

const buildBatchExportData = (dataSource: any[], currentDataObj: Record<string, any[]>) => {
  const dataList: any = currentDataObj;
  const newDataList: any = {};
  for (let i = 0; i < dataSource.length; i++) {
    const item = dataSource[i];
    if (item.checked) {
      if (!newDataList[item.stationId]) {
        newDataList[item.stationId] = [];
      }
      newDataList[item.stationId].push(item);
    }
  }

  return Object.keys(newDataList).length > 0 ? newDataList : dataList;
};

export { columns, buildTableData, downFile, buildBatchExportData };
